package com.shf.utils
{
	 import mx.controls.Alert;
	 import mx.messaging.Channel;
	 import mx.messaging.ChannelSet;
	 import mx.messaging.channels.AMFChannel;
	 import mx.rpc.AbstractOperation;
	 import mx.rpc.events.FaultEvent;
	 import mx.rpc.events.ResultEvent;
	 import mx.rpc.remoting.RemoteObject;
	 
	 public class RemoteService
	 {
		public function RemoteService()
		{
		}
		
		private static var url:String = "http://localhost:8080/shf-server"
		
		public static function execute(serviceName:String,methodName:String,param:Array,resultHandler:Function=null,faultHandler:Function=null,taobao:Boolean = false):void{
			var channelSet:ChannelSet = new ChannelSet();
			var chan:Channel = new AMFChannel(null,url+"/messagebroker/amf");
			channelSet.addChannel(chan);
			var remote:RemoteObject = new RemoteObject();
			var operator:AbstractOperation;
			if(taobao){
				remote.destination = "taobaoService";
				operator = remote.getOperation("execute");
				operator.arguments = [serviceName,methodName,Constant.SESSIONKEY,param];
			}else{
				operator = remote.getOperation(methodName);
				remote.destination = serviceName;
				operator.arguments = param;
			} 
			remote.channelSet = channelSet;
			if(resultHandler!=null)remote.addEventListener(ResultEvent.RESULT,resultHandler); 
			var faultFun:Function = faultHandler == null?defaultFaultHandler:faultHandler;
			remote.addEventListener(FaultEvent.FAULT,faultFun);
			operator.send(); 
  		}
	  
	  	private static function defaultFaultHandler(event:FaultEvent):void{
	   		Alert.show(event.message.toString());
	  	}
	  	
	 }
}